home *** CD-ROM | disk | FTP | other *** search
/ Aminet 44 / Aminet 44 (2001)(GTI - Schatztruhe)[!][Aug 2001].iso / Aminet / misc / emu / p-interp.lha / p-interp-0.4 / svolio.c < prev    next >
C/C++ Source or Header  |  2001-05-26  |  6KB  |  300 lines

  1. /*
  2.  
  3.   P-Code interpreter (to run the apple pascal system)
  4.   Copyright (C) 2000 Mario Klebsch
  5.  
  6.   This program is free software; you can redistribute it and/or modify
  7.   it under the terms of the GNU General Public License as published by
  8.   the Free Software Foundation; either version 2 of the License, or
  9.   (at your option) any later version.
  10.  
  11.   This program is distributed in the hope that it will be useful,
  12.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.   GNU General Public License for more details.
  15.  
  16.   You should have received a copy of the GNU General Public License
  17.   along with this program; if not, write to the Free Software
  18.   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20.  
  21.   $Log: svolio.c,v $
  22.   Revision 1.5  2001/05/26 15:13:29  mario
  23.   Diverse kleine Fehler behoben, fehlende #includes, Labels ohne Statement
  24.   dahinter, ...
  25.  
  26.   Revision 1.4  2001/05/21 19:06:48  mario
  27.   Mode-Parameter bei den Disk-IO-Routinen entfernt
  28.  
  29.   Revision 1.3  2001/05/20 20:35:22  mario
  30.   svolio ruft die Disk-I/O-Routinen jetzt direkt auf.
  31.  
  32.   Revision 1.2  2001/05/20 13:12:02  mario
  33.   CVS-Idents und Logs eingefügt
  34.  
  35.  
  36. */
  37.  
  38. #ident "$Id: svolio.c,v 1.5 2001/05/26 15:13:29 mario Exp $";
  39.  
  40. #include <stdio.h>
  41. #include <unistd.h>
  42. #include <stdarg.h>
  43. #include <ctype.h>
  44.  
  45. #include "psystem.h"
  46. #include "Memory.h"
  47. #include "Diskio.h"
  48.  
  49. word Syscom;
  50. word IoResult=0;
  51.  
  52. int List=0;
  53. int Text=0;
  54. int ExtList=0;
  55.  
  56. char *Month[16]={ "???", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
  57.           "Aug", "Sep", "Oct", "Nov", "Dec", "???", "???", "???"};
  58.  
  59. char *FileTypes[8]= {"Svol", "Bad ", "Code", "Text", 
  60.              "Info", "Data", "Graf", "Foto" };
  61.  
  62. void warning(char *Msg, ...)
  63. {
  64.   va_list ap;
  65.   char    Buffer[512];
  66.   va_start(ap, Msg);
  67.   vsnprintf(Buffer, sizeof(Buffer), Msg, ap);
  68.   va_end(ap);
  69.   fprintf(stderr,"warning: %s\n", Buffer);
  70. }
  71.  
  72. void XeqError(word err)
  73. {
  74.   fprintf(stderr,"XeqError: %d\n", err);
  75.   exit(-1);
  76. }
  77.  
  78. void IoError(word err)
  79. {
  80.   IoResult=err;
  81. }
  82.  
  83. void DoList(word Unit)
  84. {
  85.   int i;
  86.   int len;
  87.   word Entry;
  88.   word w;
  89.   word NumBlocks;
  90.   word NumFiles;
  91.   word FreeBlocks;
  92.   word MaxFree;
  93.   word LastBlock;
  94.   word Free;
  95.  
  96.   DiskRead(Unit, 0x100, 0, 2048, 2);
  97.   if (IoResult)
  98.     {
  99.       fprintf(stderr,"DoList: Directory read error\n");
  100.       exit(1);
  101.     }
  102.  
  103.   if (MemRd(WordIndexed(0x100,  8))&0xff00)
  104.     SwapBytes=!SwapBytes;
  105.  
  106.   Entry=0x100;
  107.   NumBlocks=MemRd(WordIndexed(Entry,  7));
  108.   LastBlock=MemRd(WordIndexed(Entry,  1));
  109.   NumFiles=MemRd(WordIndexed(Entry,  8));
  110.   FreeBlocks=0;
  111.   MaxFree=0;
  112.  
  113.   if (ExtList)
  114.     {
  115.       Entry=0x100;
  116.       for (len=0; len<MemRdByte(WordIndexed(Entry, 3),0); len++)
  117.     {
  118.       char ch=MemRdByte(WordIndexed(Entry, 3),1+len);
  119.       if (isupper(ch))
  120.         ch=tolower(ch);
  121.       putchar(ch);
  122.     }
  123.       putchar(':');
  124.       putchar('\n');
  125.     }
  126.   
  127.   for (i=0;i<NumFiles;i++)
  128.     {
  129.       Entry=WordIndexed(0x100, 13+13*i);
  130.  
  131.       if (ExtList)
  132.     if (MemRd(WordIndexed(Entry,  0))>LastBlock)
  133.       {
  134.         Free=MemRd(WordIndexed(Entry,  0))-LastBlock;
  135.         printf("< unused >     %5d           %5d\n", Free, LastBlock);
  136.         FreeBlocks += Free;
  137.         if (Free>MaxFree)
  138.           MaxFree=Free;
  139.       }
  140.       LastBlock=MemRd(WordIndexed(Entry,  1));
  141.  
  142.       for (len=0; len<MemRdByte(WordIndexed(Entry, 3),0); len++)
  143.     {
  144.       char ch=MemRdByte(WordIndexed(Entry, 3),1+len);
  145.       if (isupper(ch))
  146.         ch=tolower(ch);
  147.       putchar(ch);
  148.     }
  149.  
  150.       if (ExtList)
  151.     {
  152.       for (; len<15; len++)
  153.         putchar(' ');
  154.       
  155.       w=MemRd(WordIndexed(Entry, 12));
  156.       printf("%5d %2d-%3s-%02d %5d %5d %4sfile",
  157.          MemRd(WordIndexed(Entry, 1)) - MemRd(WordIndexed(Entry, 0)),
  158.          (w>>4)&0x1f, Month[w&0x0f], w>>9,
  159.          MemRd(WordIndexed(Entry,  0)),
  160.          MemRd(WordIndexed(Entry, 11)),
  161.          FileTypes[MemRd(WordIndexed(Entry, 2))&0x07]);
  162.     }
  163.       putchar('\n');
  164.     }
  165.  
  166.   if (ExtList)
  167.     {
  168.       if (NumBlocks>LastBlock)
  169.     {
  170.       Free=NumBlocks-LastBlock;
  171.       printf("< unused >     %5d           %5d\n", Free, LastBlock);
  172.       FreeBlocks += Free;
  173.       if (Free>MaxFree)
  174.         MaxFree=Free;
  175.     }
  176.       printf("%d files, %d blocks used, %d unused, %d in largest\n",
  177.          NumFiles, NumBlocks-FreeBlocks, FreeBlocks, MaxFree);
  178.     }
  179. }
  180.  
  181. word LookupFile(word Unit, const char *Name, word *Offset,
  182.         word *Len, word *Last)
  183. {
  184.   int i;
  185.   DiskRead(Unit, 0x100, 0, 2048, 2);
  186.   if (IoResult)
  187.     return(0);
  188.  
  189.   if (MemRd(WordIndexed(0x100,  8))&0xff00)
  190.     SwapBytes=!SwapBytes;
  191.  
  192.   for (i=0;i<MemRd(WordIndexed(0x100, 8));i++)
  193.     {
  194.       word Entry=WordIndexed(0x100, 13+13*i);
  195.       int len;
  196.       for (len=0; len<MemRdByte(WordIndexed(Entry, 3),0); len++)
  197.     if (toupper(MemRdByte(WordIndexed(Entry, 3),1+len)) !=
  198.         toupper(Name[len]))
  199.       goto next;
  200.       if (Name[len])
  201.     continue;
  202.     
  203.       *Offset=MemRd(WordIndexed(Entry,0));
  204.       *Len=MemRd(WordIndexed(Entry,1));
  205.       *Last=MemRd(WordIndexed(Entry,11));
  206.  
  207.       return(1);
  208.     next:
  209.       ;
  210.     }
  211.   return(0);
  212. }
  213.  
  214. int main(int argc, char *argv[])
  215. {
  216.   int i;
  217.  
  218.   Syscom=WordIndexed(0xfffe, -SYSCOM_SIZE);
  219.  
  220.   while ((i=getopt(argc, argv, "elt"))!=EOF)
  221.     switch(i)
  222.       {
  223.       case 'e':
  224.     ExtList=1;
  225.     List=1;
  226.     break;
  227.       case 'l':
  228.     List=1;
  229.     break;
  230.       case 't':
  231.     Text=1;
  232.     break;
  233.       }
  234.  
  235.   if (optind<argc)
  236.     DiskMount(4, argv[optind++], ReadOnly);
  237.   else
  238.     {
  239.       fprintf(stderr,"%s: Needs filename argument\n", argv[0]);
  240.       exit(1);
  241.     }
  242.  
  243.   if (List)
  244.     {
  245.       DoList(4);
  246.       exit(0);
  247.     }
  248.   if (optind<argc)
  249.     {
  250.       word Offset, End, Last;
  251.       char *name=argv[optind++];
  252.       if (!LookupFile(4, name, &Offset, &End, &Last))
  253.     {
  254.       fprintf(stderr, "%s: File not found\n", name);
  255.       exit(1);
  256.     }
  257.  
  258.       if (Text)
  259.     Offset +=2;
  260.  
  261.       while (Offset<End)
  262.     {
  263.       int Size=(End-Offset-1)*512+Last;
  264.       if (Size>1024)
  265.         Size=1024;
  266.       DiskRead(4, 0x100, 0, Size, Offset);
  267.       for (i=0; i<Size; i++)
  268.         {
  269.           char ch=MemRdByte(0x100,i);
  270.           if (Text)
  271.         {
  272.           if (ch==0x10)
  273.             {
  274.               i++;
  275.               for (ch=MemRdByte(0x100,i); ch>0x20; ch--)
  276.             write(1," ", 1);
  277.             }
  278.           else if (ch==0)
  279.             break;
  280.           else
  281.             {
  282.               if (ch==0x0d)
  283.             ch='\n';
  284.               write(1, &ch, 1);
  285.             }
  286.         }
  287.           else
  288.         write(1, &ch, 1);
  289.         }
  290.       Offset+=2;
  291.     }
  292.     }
  293.   else
  294.     {
  295.       fprintf(stderr,"%s: Needs filename argument\n", argv[0]);
  296.       exit(1);
  297.     }
  298.   exit(0);
  299. }
  300.